package com.yanqu.road.entity.util;

/**
 * 复合键（2个）
 */
public class CompositeKey {

    private final long userId1;
    private final long userId2;

    public CompositeKey(long userId1, long userId2) {
        // Ensure consistent ordering of user IDs to prevent deadlocks
        if (userId1 < userId2) {
            this.userId1 = userId1;
            this.userId2 = userId2;
        } else {
            this.userId1 = userId2;
            this.userId2 = userId1;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        CompositeKey that = (CompositeKey) o;

        if (userId1 != that.userId1) return false;
        return userId2 == that.userId2;
    }

    @Override
    public int hashCode() {
        return Long.hashCode(userId1) ^ Long.hashCode(userId2);
    }
}
